package
{
	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-27下午11:20:12
	 *
	 * 创建工文件引用类
	 * */
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoaderDataFormat;
	import flash.utils.ByteArray;
	
	import kingsAsLib.Nets.KUrlLoader;
	import kingsAsLib.air.KwriterFile;
	
	
	public class CreateClassNameFile extends Sprite
	{
		private var classArr:Vector.<String>=new Vector.<String>;
		
		private var importStrArr:Vector.<String>=new Vector.<String>;
		
		public function CreateClassNameFile()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			loadFileList();
		}
		
		private function loadFileList():void
		{
			// 
			var kurl:KUrlLoader=new KUrlLoader();
			kurl.loaderStars("flieList.txt", loadCompleteFun, null, URLLoaderDataFormat.TEXT);
		}
		
		private function loadCompleteFun(obj:String):void
		{
			
			var arr:Array=obj.split(",");
			var myPattern:RegExp=/\//gi;
			for (var i:int=0; i < arr.length; i++)
			{
				var str:String=arr[i];
				var index:int=str.lastIndexOf("/");
				
				var temp:String=str.replace(myPattern, ".");
				
				
				var temp2:String=str.substring(index+1);
				var index2:int=temp2.lastIndexOf(".");
				var temp3:String=temp2.substring(0,index2);
				
				var i_index:int=temp.lastIndexOf(".");
				importStrArr.push("import "+temp.substring(0,i_index));
				
				classArr.push(temp3);
			}
			
			createClassNameImport();
		}
		private function createClassNameImport():void
		{
			var classListNameStr:String="";
			var importStr:String="";
			for each (var i:String in classArr)
			{
				classListNameStr+=i +";"+ "\n";
			}
			
			for each (var j:String in importStrArr)
			{
				importStr+=j +";" +"\n";
			}
			
			
			
			var claStr:String="package utils" + "\n" + "{" + "\n" +importStr+ "public class ClassNameImport" + "\n" + "{" + "\n" + "public function ClassNameImport()" + "\n" + "{" + "\n" + classListNameStr + "}" + "\n" + "}" + "\n" + "}" + "\n";
			
			
			var byteArr:ByteArray=new ByteArray();
			//			byteArr.writeUTF(claStr);
			byteArr.writeMultiByte(claStr,"utf-8");
			//			trace(byteArr)
			
			var kw:KwriterFile=new KwriterFile();
			kw.saveFile(byteArr,"./ClassNameImport.as");
		}
	}
	
	//end class
}
